//
//  AppDelegate.swift
//  传智微博
//
//  Created by zhoujiming on 2016/12/15.
//  Copyright © 2016年 gmcc. All rights reserved.
//

import UIKit

import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        //sleep(2)
        
        // 应用程序额外设置
        setupAdditions()
        
        window = UIWindow()
        
        window?.backgroundColor = UIColor.white
        
        window?.rootViewController = WBMainViewController()
        
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        return true
    }

}
// 设置应用程序额外信息
extension AppDelegate{
    fileprivate func setupAdditions(){
        // 设置sv最小解除时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        
        // 设置网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        // 设置用户授权显示通知
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound, .carPlay]){(success, error) in
                print("授权 " + (success ? "成功" : "失败"))
            }
        } else {
            let notifySetting = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySetting)
        }
    }
}

extension AppDelegate{
    fileprivate func loadAppInfo(){
        DispatchQueue.global().async {
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            let data = NSData(contentsOf: url!)
            let doc = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonpath = (doc as NSString).appendingPathComponent("main.json")
            data?.write(toFile: jsonpath, atomically: true)
            print("加载完毕\(jsonpath)")
        }
    }
}

